package org.dromara.web.service.business;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.enums.DeviceType;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.dao.business.domain.UserTypeSetting;
import org.dromara.dao.business.domain.UserTypeSettingQuery;
import org.dromara.dao.business.enums.PaymentTargetEnums;
import org.dromara.dao.business.enums.PaymentTypeEnums;
import org.dromara.dao.business.mapper.UserTypeSettingMapper;
import org.dromara.web.domain.app.UserTypeReq;
import org.dromara.web.service.plugins.PaymentService;
import org.dromara.web.utils.UserTypeUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class UserTypeSettingService {

    @Autowired
    UserTypeSettingMapper userTypeSettingMapper;
    @Autowired
    PaymentService paymentService;


    public TableDataInfo<UserTypeSetting> queryPageList(UserTypeSettingQuery query, PageQuery pageQuery) {
        return new TableDataInfo(
                userTypeSettingMapper.paginationSelectList(query, pageQuery),
                userTypeSettingMapper.paginationSelectCount(query)
        );
    }


    public UserTypeSetting queryById(Long id) {
        return userTypeSettingMapper.selectById(id);
    }


    @Transactional
    public UserTypeSetting insert(UserTypeSetting entity) {
        userTypeSettingMapper.insert(entity);
        return entity;
    }

    @Transactional
    public void updateById(UserTypeSetting update) {

        UserTypeSetting entity = userTypeSettingMapper.selectById(update.getId());
        if (entity == null) {
            throw new ServiceException();
        }
        entity.setName(update.getName());
        entity.setUserType(update.getUserType());
        entity.setDescription(update.getDescription());
        entity.setDiscount(update.getDiscount());
        entity.setMonthFee(update.getMonthFee());
        entity.setYearFee(update.getYearFee());
        entity.setStatus(update.getStatus());
        entity.setUpdateTime(LocalDateTime.now());
        userTypeSettingMapper.updateById(entity);
    }


    @Transactional
    public void deleteById(Long id) {
        userTypeSettingMapper.deleteById(id);
    }

    @Transactional
    public void deleteByIds(Long[] ids) {
        userTypeSettingMapper.deleteByIds(Arrays.asList(ids));
    }



    public List<UserTypeSetting> getValidSettings() {
        return userTypeSettingMapper.selectList(new LambdaQueryWrapper<UserTypeSetting>().eq(UserTypeSetting::getStatus, true));
    }



    @Transactional
    public Object userTypeOrder(UserTypeReq.UserTypeOrderReq req) {
        UserTypeSetting userTypeSetting = userTypeSettingMapper.selectById(req.getTypeId());
        if (userTypeSetting == null) {
            throw new ServiceException("套餐已经被删除，请重新选择");
        }
        LoginUser loginUser = LoginHelper.getAppLoginUser();
        BigDecimal amount = "0".equals(req.getDuration()) ? userTypeSetting.getMonthFee(): userTypeSetting.getYearFee();

        String paymentType = "";
        if ("weixin".equals(req.getType())) {
            if (DeviceType.PC.getDevice().equals(loginUser.getDeviceType())) {
                paymentType = PaymentTypeEnums.TYPE_WX_NATIVE;
            } else {
                paymentType = PaymentTypeEnums.TYPE_WX_H5;
            }
        } else if ("alipay".equals(req.getType())) {
            if (DeviceType.PC.getDevice().equals(loginUser.getDeviceType())) {
                paymentType = PaymentTypeEnums.TYPE_ALIPAY_PC;
            } else {
                paymentType = PaymentTypeEnums.TYPE_ALIPAY_H5;
            }
        }


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("settingId", userTypeSetting.getId());
        jsonObject.put("amount", amount);
        jsonObject.put("duration", req.getDuration());
        jsonObject.put("userType", userTypeSetting.getUserType());

        return  paymentService.createOrder(loginUser.getUserId(), PaymentTargetEnums.TARGET_VIP, paymentType,amount, jsonObject.toJSONString());

    }
}
